/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.uca.sisbec.actions.becario;

import com.uca.sisbec.beans.MateriaXBecarioBean;
import com.uca.sisbec.commons.HibernateAction;
import com.uca.sisbec.forms.becario.RegistroMateriaForm;
import com.uca.sisbec.forms.manto.CarreraForm;
import com.uca.sisbec.helpers.becario.RegistroMateriasHelper;
import com.uca.sisbec.helpers.manto.CarreraHelper;
import com.uca.sisbec.mapping.Becario;
import com.uca.sisbec.mapping.MateriaXBecario;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.*;
import org.hibernate.Transaction;

/**
 *
 * @author Susy
 */
public class RegistroMateriaAction extends HibernateAction{
    private List<MateriaXBecarioBean> listaMat;
    
    public ActionForward init(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        if (!isBecarioInSession(request)) return mapping.findForward("searchBecario");
        RegistroMateriaForm mf = (RegistroMateriaForm) form;
        mf.setEstado(true);
        return mapping.findForward("success");
    }
    
    public ActionForward addMateria(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        RegistroMateriaForm mf = (RegistroMateriaForm) form;
        mf.setEstado(true);
        RegistroMateriasHelper helper = new RegistroMateriasHelper(getSession(request));
        request.setAttribute("CarrerasList", helper.getAllCarreras(mf));
        request.setAttribute("UniversidadesList", helper.getUniversidadesActivas());
        request.setAttribute("MateriasList",helper.getAllMaterias(mf));
        return mapping.findForward("add");
    }
    
    public ActionForward guardarMateria(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        RegistroMateriaForm mf = (RegistroMateriaForm) form;
        ActionMessages msg = new ActionMessages();
        ActionErrors errors = new ActionErrors();
        RegistroMateriasHelper helper = new RegistroMateriasHelper(getSession(request));
        if(!helper.existeCodMateria(mf)){
            helper.agregarMateria(mf);
        request.setAttribute("CarrerasList", helper.getAllCarreras(mf));
        request.setAttribute("UniversidadesList", helper.getUniversidadesActivas());
        request.setAttribute("MateriasList",helper.getAllMaterias(mf));
        msg.add(ActionMessages.GLOBAL_MESSAGE,new ActionMessage("materia.nueva"));
        }else{
            errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("materia.existe",mf.getCodMateria() ));
            request.setAttribute("errors", errors);
            request.setAttribute("CarrerasList", helper.getAllCarreras(mf));
            request.setAttribute("UniversidadesList", helper.getUniversidadesActivas());
            request.setAttribute("MateriasList",helper.getAllMaterias(mf));
        }
        
        return mapping.findForward("add");
    }
    
    public ActionForward searchMaterias(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        RegistroMateriaForm mf = (RegistroMateriaForm) form;
        RegistroMateriasHelper helper = new RegistroMateriasHelper(getSession(request));
        request.setAttribute("CarrerasList", helper.getAllCarreras(mf));
        request.setAttribute("UniversidadesList", helper.getUniversidadesActivas());
        request.setAttribute("MateriasList",helper.buscarMateriaDB(mf));
        return mapping.findForward("add");
    }
    
    public ActionForward cargarMaterias(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        RegistroMateriaForm mf = (RegistroMateriaForm) form;
        RegistroMateriasHelper mh = new RegistroMateriasHelper(getSession(request));
        Becario b = (Becario) request.getSession().getAttribute(_BECARIO);
        request.setAttribute("materiasList", mh.obtenerMateriasXCarrera(mf,b));
        //listaMat=mh.buscarMateriasBecario(mf,b);
        request.getSession().setAttribute(_MATERIA,mh.buscarMateriasBecario(mf,b));
        request.setAttribute(_MATERIA,request.getSession().getAttribute(_MATERIA));
        request.setAttribute("load","carga");
        System.out.println("Llegue aqui");
        return mapping.findForward("success");
        
    }
    
     public ActionForward agregarMateriaLista(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        RegistroMateriaForm mf = (RegistroMateriaForm) form;
        Becario b = (Becario) request.getSession().getAttribute(_BECARIO);
        RegistroMateriasHelper mh = new RegistroMateriasHelper(getSession(request));
        //listaMat.add(mh.obtenerObjMateriaXBecario(mf,b));
        List<MateriaXBecarioBean> list = (ArrayList<MateriaXBecarioBean>) request.getSession().getAttribute(_MATERIA);
        list.add(mh.obtenerObjMateriaXBecario(mf,b));
        request.getSession().setAttribute(_MATERIA, list);
        request.setAttribute("materiasList", mh.obtenerMateriasXCarrera(mf,b));
        request.setAttribute(_MATERIA,list);
        request.setAttribute("load","carga");
        System.out.println("Llegue aqui");
        return mapping.findForward("success");   
    }
    
      public ActionForward guardarMaterias(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        RegistroMateriaForm mf = (RegistroMateriaForm) form;
        Becario b = (Becario) request.getSession().getAttribute(_BECARIO);
        RegistroMateriasHelper mh = new RegistroMateriasHelper(getSession(request));
        List<MateriaXBecarioBean> list = (ArrayList<MateriaXBecarioBean>) request.getSession().getAttribute(_MATERIA);
        //list.add(mh.obtenerObjMateriaXBecario(mf,b));
        request.getSession().setAttribute(_MATERIA, list);
        mh.deleteMaterias(mf,b);
        
        getTx(request).commit();
        Transaction tx = getSession(request).beginTransaction();
        request.getSession().setAttribute("_TRX_HIB", tx);
        
        mh.guardarMateriasXBecario(list,b);
        //listaMat= mh.buscarMateriasBecario(mf,b);
        request.setAttribute("materiasList", mh.obtenerMateriasXCarrera(mf,b));
         request.setAttribute(_MATERIA, list);
        request.setAttribute("load","carga");
        return mapping.findForward("success");
        
    }
    
    public ActionForward InhabilitarMateria(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        RegistroMateriaForm mf = (RegistroMateriaForm) form;
        ActionMessages msg = new ActionMessages();
        Becario b = (Becario) request.getSession().getAttribute(_BECARIO);
        RegistroMateriasHelper mh = new RegistroMateriasHelper(getSession(request));
        mh.InhabilitarMateriaDB(request.getParameter("cod"));
        getSession(request).flush();
        request.setAttribute("materiasList", mh.obtenerMateriasXCarrera(mf,b));
         request.setAttribute("materiaData",listaMat);
        request.setAttribute("load","carga");
        msg.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("materia.habilitada"));
        request.setAttribute("msg", msg);
        return mapping.findForward("success");
    }
    
    public ActionForward HabilitarMateria(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        ActionMessages msg = new ActionMessages();
        RegistroMateriaForm mf = (RegistroMateriaForm) form;
        Becario b = (Becario) request.getSession().getAttribute(_BECARIO);
        RegistroMateriasHelper mh = new RegistroMateriasHelper(getSession(request));
        mh.HabilitarMateriaDB(request.getParameter("cod"));
        getSession(request).flush();
        request.setAttribute("materiasList", mh.obtenerMateriasXCarrera(mf,b));
        request.setAttribute("materiaData",listaMat);
        request.setAttribute("load","carga");
        msg.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("materia.habilitada"));
        request.setAttribute("msg", msg);
        return mapping.findForward("success");
    } 
    
    public ActionForward EliminarMatxBec(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        RegistroMateriasHelper mh = new RegistroMateriasHelper(getSession(request));
        RegistroMateriaForm mf = (RegistroMateriaForm) form;
        Becario b = (Becario) request.getSession().getAttribute(_BECARIO);
        List<MateriaXBecarioBean> list = (ArrayList<MateriaXBecarioBean>) request.getSession().getAttribute(_MATERIA);
        String id=request.getParameter("carnet")+"|"+request.getParameter("cod")+"|"+request.getParameter("correl");
        for(int i=0;i<list.size();i++){
            if(id.equals(list.get(i).getIdCompleto())){
                list.remove(i);
                break;
            }
        }
        request.getSession().setAttribute(_MATERIA, list);
        //mh.EliminarMatXBecDB(request.getParameter("carnet"), request.getParameter("cod"),Integer.parseInt(request.getParameter("correl")));
        //getSession(request).flush();
        //listaMat=mh.buscarMateriasBecario(mf,b);
        request.setAttribute("materiasList", mh.obtenerMateriasXCarrera(mf,b));
        request.setAttribute(_MATERIA,list);
        request.setAttribute("load","carga");
        return mapping.findForward("success");
    }
    
}
